import {createRouter, createWebHistory} from 'vue-router'

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/login',
            name: 'login',
            component: () => import("@/views/Login/index.vue"),
            meta: {
                title: "登录"
            },
            beforeEnter: (to, from) => {
                const user = JSON.parse(localStorage.getItem("user"))
                if (user&&user.token) {
                    ElMessage.warning("请先退出登录")
                    return false
                }
            },
        },
        {
            path: '/',
            name: 'layout',
            component: () => import("@/views/Layout/index.vue"),
            children: [
                {
                    path: "/",
                    name: "home",
                    component: () => import("@/views/Home/index.vue"),
                    meta: {
                        title: "首页",
                        isKeepAlive: true,
                        header:true
                    },
                },
                {
                    path: "inform",
                    name: "inform",
                    component: () => import("@/views/Inform/index.vue"),
                    meta: {
                        title: "通知公告",
                        isKeepAlive: true,
                        header:true
                    },
                },
                {
                    path: "project",
                    name: "project",
                    components: {
                        default: () => import("@/views/Project/index.vue"),
                    },
                    meta: {
                        title: "大创项目",
                        isKeepAlive: true,
                        header:true
                    }
                },
                {
                    path: "preview/project/:id",
                    name: "previewProject",
                    component: () => import("@/views/Preview/Project/index.vue"),
                    props: true,
                    meta: {
                        title: "项目详情",
                        isKeepAlive: true,
                        header:false
                    }
                },
                {
                    path: "preview/myproject/:id",
                    name: "previewMyProject",
                    component: () => import("@/views/Preview/ProjectDB/index.vue"),
                    props: true,
                    meta: {
                        title: "项目详情",
                        isKeepAlive: true,
                        header:false
                    }
                },
                {
                    path: "preview/inform/:id",
                    name: "previewInform",
                    component: () => import("@/views/Preview/Inform/index.vue"),
                    props: true,
                    meta: {
                        title: "通知公告详情",
                        isKeepAlive: true,
                        header:false
                    }
                },
                {
                    path: "preview/file/:url",
                    name: "previewFile",
                    component: () => import("@/views/Preview/File/index.vue"),
                    props: true,
                    meta: {
                        title: "文件预览",
                        isKeepAlive: true,
                        header:false
                    }
                },
                {
                    path: "receipt/manage/:id",
                    name: "receiptManage",
                    component:() => import("@/views/ReceiptManage/index.vue"),
                    props: true,
                    meta: {
                        title: "发票管理",
                        isKeepAlive: true,
                        header:false
                    }
                },
                {
                    path: "user",
                    name: "user",
                    component: () => import("@/views/User/index.vue"),
                    props: true,
                    meta:{
                        title:"个人中心",
                        isKeepAlive:true,
                        header:true
                    },
                    children: [
                        {
                            path: "myProject/:id",
                            name: "myProject",
                            component:() => import("@/views/User/MyProject/Index.vue"),
                            props: true,
                            meta: {
                                title: "我的项目",
                                isKeepAlive: true
                            }
                        },
                        {
                            path: "involveProject/:id",
                            name: "involveProject",
                            component:() => import("@/views/User/InvolveProject/Index.vue"),
                            props: true,
                            meta: {
                                title: "参与项目",
                                isKeepAlive: true
                            }
                        },
                        {
                            path: "receipt/:id",
                            name: "receipt",
                            component:() => import("@/views/User/Receipt/index.vue"),
                            props: true,
                            meta: {
                                title: "发票报销",
                                isKeepAlive: true
                            }
                        },
                        {
                            path: "myInform/:id",
                            name: "myInform",
                            component:() => import("@/views/User/MyInform/index.vue"),
                            props: true,
                            meta: {
                                title: "我的公告",
                                isKeepAlive: true
                            }
                        },
                        {
                            path: "myDirect/:id",
                            name: "myDirect",
                            component:() => import("@/views/User/MyDirect/index.vue"),
                            props: true,
                            meta: {
                                title: "我的第一指导项目",
                                isKeepAlive: true
                            }
                        },
                        {
                            path: "mySecondDirect/:id",
                            name: "mySecondDirect",
                            component:() => import("@/views/User/MySecondDirect/index.vue"),
                            props: true,
                            meta: {
                                title: "我的第二指导项目",
                                isKeepAlive: true
                            }
                        },
                        {
                            path: "info/:id",
                            name: "info",
                            component:() => import("@/views/User/Info/index.vue"),
                            props: true,
                            meta: {
                                title: "个人资料",
                                isKeepAlive: true
                            }
                        },
                        {
                            path: "account/:id",
                            name: "account",
                            component:() => import("@/views/User/Account/Index.vue"),
                            props: true,
                            meta: {
                                title: "账号管理",
                                isKeepAlive: true
                            }
                        }
                    ]
                }
            ]
        }
    ]
})

//为面包屑导航带有参数的匹配路径matched数组中的路由信息添加全路径
router.beforeEach((to, from) => {
    // if (to.params || to.query) {
    //     to.matched[to.matched.length - 1].fullPath = to.fullPath
    // }
})
router.afterEach((to, from) => {
    document.title = to.meta.title
})
export default router
